<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="com.eos.data.datacontext.IUserObject"%>
<%@ page import="com.eos.data.datacontext.IMUODataContext"%>
<%@ page import="com.eos.data.datacontext.DataContextManager"%>
<html><head><title>upFile</title></head>
<body bgcolor="#ffffff">
<%
//定义上载文件的最大字节
int MAX_SIZE = 1024 * 1024;
// 创建根路径的保存变量
String rootPath;
//声明文件读入类
DataInputStream in = null;
FileOutputStream fileOut = null;
//取得客户端的网络地址
String remoteAddr = request.getRemoteAddr();

//获得服务器的名字
String serverName = request.getServerName();


//取得互联网程序的绝对地址
String realPath = request.getRealPath(serverName);

realPath = realPath.substring(0,realPath.lastIndexOf("\\"));

//创建文件的保存目录
rootPath = "\\C:upload\\";

//取得客户端上传的数据类型
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data") >= 0){
//读入上传的数据
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE){
out.println("<P>上传的文件字节数不可以超过" + MAX_SIZE + "</p>");
return;
}
//保存上传文件的数据
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//上传的数据保存在byte数组
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
//根据byte数组创建字符串
String file = new String(dataBytes);
//out.println(file);
//取得上传的数据的文件名
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
String lj=rootPath+saveFile;
System.out.println(saveFile);
int lastIndex = contentType.lastIndexOf("=");
System.out.println(lastIndex);
//取得数据的分隔字符串
String boundary = contentType.substring(lastIndex + 1,contentType.length());
System.out.println(boundary);
//创建保存路径的文件名
String fileName = rootPath + saveFile;
IMUODataContext muo = DataContextManager.current().getMUODataContext();
		IUserObject userobject = muo.getUserObject();
		String  userName= userobject.getUserName();
	System.out.println(userName);
	String status="1";	
//out.print(fileName);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
int boundaryLocation = file.indexOf(boundary,pos) - 4;
//out.println(boundaryLocation);
//取得文件数据的开始的位置
int startPos = ((file.substring(0,pos)).getBytes()).length;
//out.println(startPos);
//取得文件数据的结束的位置
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
//out.println(endPos);
//检查上载文件是否存在
File checkFile = new File(fileName);
if(checkFile.exists()){
out.println("该文件已存在。。。");
}else{
System.out.println("11111111111");
       Connection conn=null;  
       Statement ps=null;        
            Class.forName("oracle.jdbc.driver.OracleDriver");//找到oracle驱动器所在的类  
            String url="jdbc:oracle:thin:@localhost:1521:orcl"; //URL地址  
            String username="system";  
            String password="Fyl123456";  
            conn=DriverManager.getConnection(url, username, password);  
             try { 
             String sql = "insert into zhmy_attachment(ATT_FOUNDER,ATT_STATUS,ATT_CTIME,ATT_ADDRESS)"  
                         + " values ('"+ userName+"', '1',to_char(sysdate,'YYYY-MM-DD'),'"+lj+"')"; // 插入数据的sql语句  
                                                 
            // 创建用于执行静态sql语句的Statement对象  
            System.out.println("2222222222222");
         	ps = conn.createStatement();
			ps.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		} 
}
//检查上载文件的目录是否存在
File fileDir = new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
//创建文件的写出类
fileOut = new FileOutputStream(fileName);
//保存文件的数据
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close();
out.println(saveFile + "文件成功上载.</p>");
}else{
String content = request.getContentType();
out.println("<p>上传的数据类型不是multipart/form-data</p>");
}
}catch(Exception ex){
throw new ServletException(ex.getMessage());
}
%>
</body>
</html>